#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <mqueue.h>
#include <string.h>
using namespace std;
int main(int argc, char const *argv[])
{
    char* name = "/producer";
    struct mq_attr attr;
    attr.mq_curmsgs = 0;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 100;
    mqd_t mqid = mq_open(name,O_RDWR | O_CREAT,0664,&attr);

    while(1){
        char buffer[1024];
        ssize_t num = read(STDIN_FILENO,buffer,1024);
        if(num>0){
            mq_send(mqid,buffer,num,0);
        }else if(num == 0){
            cout << "退出生产者进程" << endl;
            char eof = EOF;
            mq_send(mqid,&eof,1,0);
            break;
        }else{
            perror("读入数据错误，退出进程！");
            exit(EXIT_FAILURE);
        }
    }

    return 0;
}
